﻿using AutoMapper;
using Work.API.Applications.Write.Commands;
using Work.API.Dtos;
using Work.API.Dtos.DrugManagement;
using Work.Domain;
using Work.Domain.DrugManage;

namespace Work.API
{
    /// <summary>
    /// 对象关系自动映射
    /// </summary>
    public class MappingProfiles:Profile
    {
        public MappingProfiles()
        {
            // 患者相关映射
            CreateMap<AddPatientCommand, Patient>().ReverseMap();
            
            // 预约相关映射
            CreateMap<CreateAppointmentCommand, Appintment>().ReverseMap();
            CreateMap<Appintment, AppointmentListDto>()
                .ForMember(dest => dest.PatientName, opt => opt.Ignore())
                .ForMember(dest => dest.Gender, opt => opt.Ignore())
                .ForMember(dest => dest.Age, opt => opt.Ignore())
                .ForMember(dest => dest.Phone, opt => opt.Ignore());
            
            // 病历相关映射
            CreateMap<MedicalRecord, MedicalRecordDto>().ReverseMap();
            CreateMap<CreateMedicalRecordCommand, MedicalRecord>()
                .ForMember(dest => dest.RecordId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.Ignore());
            
            // 处方明细相关映射
            CreateMap<PrescriptionItem, PrescriptionItemDto>().ReverseMap();

            // ============================================
            // 药品管理模块映射
            // ============================================

            // 药品基础信息映射
            CreateMap<Drug, DrugDto>().ReverseMap();

            // 药品入库记录映射
            CreateMap<DrugInbound, DrugInboundDto>()
                .ForMember(dest => dest.DrugName, opt => opt.MapFrom(src => src.Drug != null ? src.Drug.DrugName : null));

            // 药品出库记录映射
            CreateMap<DrugOutbound, DrugOutboundDto>()
                .ForMember(dest => dest.DrugName, opt => opt.MapFrom(src => src.Drug != null ? src.Drug.DrugName : null));

            // 已发药品映射（从DrugOutbound映射）
            CreateMap<DrugOutbound, DispensedDrugDto>()
                .ForMember(dest => dest.DrugName, opt => opt.MapFrom(src => src.Drug != null ? src.Drug.DrugName : null));

            // 检药单映射
            CreateMap<DrugInspection, InspectionListDto>()
                .ForMember(dest => dest.Gender, opt => opt.Ignore())
                .ForMember(dest => dest.Age, opt => opt.Ignore())
                .ForMember(dest => dest.IdCard, opt => opt.Ignore());
        }
    }
}
